package de.oszimtcc.timetableservices;

import java.util.Calendar;
import java.util.Date;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import de.oszimtcc.framework.BaseActivity;
import de.oszimtcc.framework.BaseScreen;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetable.TimetableBaseScreen;
import de.oszimtcc.timetable.R.drawable;
import de.oszimtcc.timetable.R.string;
import de.oszimtcc.timetablecore.Block;
import de.oszimtcc.timetablecore.Day;
import de.oszimtcc.timetablecore.Timetable;
import de.oszimtcc.timetablehelper.ApiWebservice;
import de.oszimtcc.timetablehelper.RequestCache;
import de.oszimtcc.timetablehelper.XMLfunctions;
import de.oszimtcc.timetablescreens.TimetableScreen;


import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;


public class Notifications 
{

    private static final int SERVICE_ID = R.string.app_name;
	private Context context;
	private int request_code = 19051986;
	
	public Notifications(Context context) 
	{
		this.context = context;
	}	

    public void setAlarmManager() 
    {   
		
    	Logger.Debug("request to oszimtcc-api");
        String xml = ApiWebservice.GetRandomChangePlan();
        
        Timetable changes = new Timetable(xml);
        Timetable old = RequestCache.getInstance(TimetableApplication.getInstance().getActivity().GetStringPreference("Class")).GetLatestTimetable();
        Boolean smthChanged = old.CompareAndSaveChanges(changes);
     
        RequestCache.getInstance(TimetableApplication.getInstance().getActivity().GetStringPreference("Class")).ChangeLastTimetable(old);
		
		if(smthChanged)
			notify(context, "Der Test klappt");
		
		//In reality, you would want to have a static variable for the request code instead of 192837
		//PendingIntent sender = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		//PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
		//long l = SystemClock.uptimeMillis();
		//powerManager.userActivity(l, false);

		
    }
    
    public static void notify(Context ctx, String desc) 
    {
        try 
        {
                Notification note = new Notification(R.drawable.timetableicon, null, 0);
                PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx, TimetableBaseScreen.class), 0);
                note.flags |= Notification.FLAG_AUTO_CANCEL;

                long[] vibrate = {5,5,5,5};
                note.vibrate = vibrate;
                
                CharSequence title = ctx.getText(R.string.app_name);
                note.setLatestEventInfo(ctx, title, desc, pi);
                NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
                nm.notify(SERVICE_ID, note);
                
        } 
        catch (Exception e) 
        {
        	Logger.Error(e.getMessage());
        }
    }

}
